package carassius.GUI;

import carassius.DAL.AnimalRow;
import carassius.DAL.OrganismListRow;
import java.awt.Component;
import java.awt.Window;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author siebz0r
 */
public class AnimalTableCellProcessor
		extends AbstractCellEditor
		implements TableCellEditor,
		TableCellRenderer
{
	private final AnimalInfoSmallPanel PANEL;
	private Object _value;

	public AnimalTableCellProcessor(Window parent)
	{
		PANEL = new AnimalInfoSmallPanel(parent);
	}

	@Override
	public Object getCellEditorValue()
	{
		return _value;
	}

	@Override
	public Component getTableCellEditorComponent(JTable table, Object value,
												 boolean isSelected, int row,
												 int column)
	{
		_value = value;
		return getTableCellRendererComponent(table, value, isSelected, true, row, column);
	}

	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
												   boolean isSelected,
												   boolean hasFocus, int row,
												   int column)
	{
		if (value instanceof AnimalRow)
		{
			PANEL.setAnimalRow((AnimalRow) value);
		}
		else if (value instanceof OrganismListRow)
		{
			PANEL.setOrganismListRow((OrganismListRow) value);
		}
		if (isSelected || hasFocus)
		{
			PANEL.setForeground(UIManager.getColor("List.selectionForeground"));
			PANEL.setBackground(UIManager.getColor("List.selectionBackground"));
		}
		else
		{
			PANEL.setForeground(UIManager.getColor("Panel.foreground"));
			PANEL.setBackground(UIManager.getColor("Panel.background"));
		}
		return PANEL;
	}
}
